package com.mall.seckil.scheduled;

import com.mall.seckil.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author: 谢君臣
 * @Date: 2022/9/18-16:09
 * @version: 1.0
 * @function:
 */
@Slf4j
@Service
public class SeckillScheduled {

    @Resource
    private SeckillService seckillService;
    @Resource
    private RedissonClient redissonClient;

    private final String SCHEDULED_LOCK = "seckill:upload:lock";

//    @Async
    @Scheduled(cron = "0 30 * * * ?")
    public void uploadSeckillSkuLatest3Days(){
        log.info("执行上架秒杀任务");
        RLock lock = redissonClient.getLock(SCHEDULED_LOCK);
        try {
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSeckillSkuLatest3Days();
        }finally {
            lock.unlock();
        }
    }

}
